﻿using System;
using System.Collections.Generic;
using System.Linq;
using CoreShop.Library;
using CoreShop.Model;
using CoreShop.Repository;
using SqlSugar;

namespace CoreShop.Services
{
    /// <summary>
    /// 接口实现
    /// </summary>
    public class corecmsprintbilltemplateServices : Repository<corecmsprintbilltemplate>, IcorecmsprintbilltemplateServices
    {
        public bool InsertAsync(corecmsprintbilltemplate entity)
        {
            return base.Change<corecmsprintbilltemplate>().Insert(entity);
        }
        public bool InsertOrUpdate(List<corecmsprintbilltemplate> entity)
        {
            return base.InsertOrUpdate(entity);
        }
        /// <summary>
        /// 重写异步更新方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(corecmsprintbilltemplate entity)
        {
            return base.Change<corecmsprintbilltemplate>().Update(a => new corecmsprintbilltemplate()
            {
                billtypeId = entity.billtypeId,
                foodPrintType = entity.foodPrintType,
                name = entity.name,
                content = entity.content,
                reportFile = entity.reportFile,
                reportData = entity.reportData,
                taskSign = entity.taskSign,
            }, a => a.id == entity.id);
        }

        /// <summary>
        /// 重写异步更新方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(List<corecmsprintbilltemplate> entity)
        {
            bool returnKey = true;
            foreach (var item in entity)
            {
                returnKey = base.Change<corecmsprintbilltemplate>().Update(a => new corecmsprintbilltemplate()
                {
                    billtypeId = item.billtypeId,
                    name = item.name,
                    foodPrintType = item.foodPrintType,
                    content = item.content,
                    reportFile =item.reportFile,
                    reportData =item.reportData,
                    taskSign = item.taskSign,  
                }, a => a.id == item.id);
            }
            return returnKey;
        }

        /// <summary>
        /// 重写删除指定ID的数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteByIdAsync(long id)
        {
            return base.Change<corecmsprintbilltemplate>().DeleteById(id);
        }

        /// <summary>
        /// 重写删除指定ID集合的数据(批量删除)
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool DeleteByIdsAsync(int[] ids)
        {
            bool returnKey = true;
            foreach (int id in ids)
            {
                returnKey = base.Change<corecmsprintbilltemplate>().DeleteById(id);
            }
            return returnKey;
        }

        #region 获取缓存的所有数据==========================================================
        /// <summary>
        /// 获取缓存的所有数据
        /// </summary>
        /// <returns></returns>
        public List<corecmsprintbilltemplate> GetCaChe()
        {
            return base.GetList();
        }
        #endregion


        #region 根据条件查询分页数据
        /// <summary>
        /// 根据条件查询分页数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public List<corecmsprintbilltemplate> QueryPageAsync(ref int count, int? pageIndex, int? pageSize, string sName)
        {
            List<corecmsprintbilltemplate> custos = new List<corecmsprintbilltemplate>();
            var where = Expressionable.Create<corecmsprintbilltemplate>();
            //if (!string.IsNullOrEmpty(sName))
            //{
            //    where = where.And(a => a.gearName.Contains(sName));
            //}
            if (pageIndex >= 1 && pageSize > 0)
            {
                custos = base.AsQueryable().WhereIF(true, where.ToExpression())
                    .ToPageList((int)pageIndex, (int)pageSize, ref count)
                    .OrderBy(a => a.id).ToList();
            }
            else
            {
                custos = base.AsQueryable().WhereIF(true, where.ToExpression()).OrderBy(a => a.id).ToList();
            }
            return custos;
        }
        #endregion

        #region
        /// <summary>
        /// 根据条件查询数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public List<corecmsprintbilltemplate> QueryAsync(long typeId, corecmssystemsetup systemsetup)
        {
            List<corecmsprintbilltemplate> list = new List<corecmsprintbilltemplate>();
            var where = Expressionable.Create<corecmsprintbilltemplate>();
            if (typeId > 0)
            {
                where = where.And(a => a.billtypeId == typeId);
            }
            else
            if (!systemsetup.isKitchen)
            {
                //如果不启用厨房打印，不显示厨房打印方案名称
                var where1 = Expressionable.Create<corecmsprintbilltype>();
                where1 = where1.And(p => p.tagId != (int)GlobalEnumVars.PrintbillTypeTag.Kitchen);
                var billtypeIds = new corecmsprintbilltypeServices().AsQueryable()
                    .Where(where1.ToExpression()).ToList().Select(p => p.id).ToList();

                where = where.And(a => billtypeIds.Contains(a.billtypeId));
            }

            list = base.AsQueryable().WhereIF(true, where.ToExpression()).OrderBy(a => a.billtypeId).ToList();
            return list;
        }
        #endregion
        /// <summary>
        /// 根据id查询单条
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public corecmsprintbilltemplate SelectById(int id)
        {
            return base.Change<corecmsprintbilltemplate>().GetSingle(a => a.id == id);
        }
                
    }
}